package com.smartwasser.yunhtgl.apphtgl.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dou361.dialogui.listener.DialogUIListener;
import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseConstant;
import com.smartwasser.yunhtgl.apphtgl.bean.CaseCensor1Bean;
import com.smartwasser.yunhtgl.apphtgl.presenter.CaseCensorPresenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.CaseCensorPresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.view.CaseCensorView;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.dou361.dialogui.DialogUIUtils.showAlertHorizontal;
import static com.smartwasser.yunhtgl.apphtgl.R.id.case_censor_confirm;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;

/**
 * 合同备案详情
 */
public class CaseCensorActivity extends BaseActivity implements CaseCensorView {
    private CaseCensorPresenter caseCensorPresenter = new CaseCensorPresenterImpl(this);
    private Dialog loadingHorizontal;
    private Intent intent;
    private String type;
    private String id;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.case_censor)
    LinearLayout case_censor;
    @BindView(R.id.case_censor_check_text)
    TextView case_censor_check_text;
    @BindView(R.id.case_censor_feed)
    EditText case_censor_feed;
    @BindView(R.id.case_censor_check)
    CheckBox case_censor_check;
    @BindView(R.id.case_censor_feedBack)
    LinearLayout case_censor_feedBack;
    @BindView(R.id.case_censor_name)
    TextView case_censor_name;
    @BindView(R.id.case_censor_code)
    TextView case_censor_code;
    @BindView(R.id.case_censor_type)
    TextView case_censor_type;
    @BindView(R.id.case_censor_shouzhi)
    TextView case_censor_shouzhi;
    @BindView(R.id.case_censor_jia)
    TextView case_censor_jia;
    @BindView(R.id.case_censor_main)
    TextView case_censor_main;
    @BindView(R.id.case_censor_bd)
    TextView case_censor_bd;
    @BindView(R.id.case_censor_money)
    TextView case_censor_money;
    @BindView(R.id.case_censor_time)
    TextView case_censor_time;
    @BindView(R.id.case_censor_create)
    TextView case_censor_create;


    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_case_censor);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        intent = getIntent();
        type = intent.getStringExtra("type");
        if ("unFinish".equals(type)) {
            tv_toolbar.setText("合同备案审核");
            case_censor_feedBack.setVisibility(View.VISIBLE);
        } else if ("finish".equals(type)) {
            tv_toolbar.setText("合同备案详情");
            case_censor_feedBack.setVisibility(View.GONE);
        }
        id = intent.getStringExtra("id");
        Logger.e("id = " + id);
        case_censor_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean boom) {
                if (boom) {
                    case_censor_check_text.setText("不通过");
                    case_censor_feed.setText("");
                } else {
                    case_censor_check_text.setText("通过");
                    case_censor_feed.setText("通过");
                }
            }
        });
    }

    @Override
    public void initData() {
        try {
            loadingHorizontal = showLoadingHorizontal(this, "加载中...").show();
            caseCensorPresenter.getMessages(getStringData(this, "JSESSIONID", ""), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.back, case_censor_confirm})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                setResult(BaseConstant.successSPGreat1SH);
                finish();
                break;
            case case_censor_confirm:
                caseCensorDialog();
                break;
        }
    }

    public void caseCensorDialog() {
        if (case_censor_feed.getText().toString().trim().isEmpty()) {
            showResult("请输入审核意见。");
            return;
        }
        showAlertHorizontal(this, "审核提示", "确定要审核该备案吗？", new DialogUIListener() {
            @Override
            public void onPositive() {
                loadingHorizontal = showLoadingHorizontal(getApplication(), "审核中...").show();
                String JSESSIONID = getStringData(CaseCensorActivity.this, "JSESSIONID", "");
                String comment = case_censor_feed.getText().toString().trim();
                String flagF;
                if (case_censor_check.isChecked()) {
                    flagF = "no";
                } else {
                    flagF = "yes";
                }
                Logger.e("JSESSIONID = " + JSESSIONID + "  comment = " + comment + "  flagF" + flagF);
                caseCensorPresenter.shMessage(JSESSIONID, id, flagF, comment);
            }

            @Override
            public void onNegative() {

            }
        }).show();
    }

    @Override
    public void success(CaseCensor1Bean caseCensorBean) {
        case_censor_name.setText(caseCensorBean.getVo().getName());
        case_censor_code.setText(caseCensorBean.getVo().getCode());
        case_censor_type.setText(caseCensorBean.getVo().getType());
        case_censor_shouzhi.setText(caseCensorBean.getVo().getShouzhi());
        case_censor_jia.setText(caseCensorBean.getVo().getOppositeParty());
        case_censor_main.setText(caseCensorBean.getVo().getMainContent());
        case_censor_bd.setText(caseCensorBean.getVo().getContractObject());
        case_censor_money.setText(caseCensorBean.getVo().getContractValue());
        case_censor_time.setText(caseCensorBean.getVo().getExecutionPeriod());
        case_censor_create.setText(caseCensorBean.getVo().getSignDate().toString());
        case_censor.setVisibility(View.VISIBLE);
        dismiss(loadingHorizontal);
    }

    @Override
    public void error() {
        dismiss(loadingHorizontal);
        showError();
    }

    @Override
    public void successSH() {
        dismiss(loadingHorizontal);
        showResult("审核成功");
        setResult(BaseConstant.successSPGreat1SH);
        finish();
    }

    @Override
    public void errorSH() {
        dismiss(loadingHorizontal);
        showResult("审核失败");
    }
}